$.expendingLi = "";
var onCustomizeclick = function(obj) {
	$("#urlDisplay").empty();
	var db = window.openDatabase("RssDB", "1.0", "Rss DB", 1000000);
	
	function querySuccess(tx, results){
//		alert("success and " + (typeof results.rows) + " rows length is " results.rows.length);  showGroup(\'_url_List'+results.rows.item(i).gid+'\');
		if((typeof results.rows)!=="undifined"&&results.rows.length>0){
			for (var i=0; i<results.rows.length; i++){
		  	  var groupDiv = document.createElement('div');
			  var groupHeader = document.createElement('h2');
			  var displayStutas = "none";
			  if($.expendingLi===(""+i)){
				  displayStutas = "";
				  groupHeader.setAttribute('class','expanded');
			  }
			  else
				  groupHeader.setAttribute('class','collapsed');
			  groupHeader.setAttribute('onclick','changingSelect(this,'+i+');showHide(this,\'_url_List'+results.rows.item(i).gid+'\');');
			  groupHeader.innerHTML=results.rows.item(i).name;
			  groupDiv.appendChild(groupHeader);
			  showGroup(results.rows.item(i).gid,groupHeader,db,displayStutas);
			  $("#urlDisplay").append(groupDiv);
			}
			if($.expendingLi.length!==0){
				
			}
		}
		if (!results.rowsAffected) {
		  return false;
		}
	}
	
	db.transaction(function(tx){
		tx.executeSql('CREATE TABLE IF NOT EXISTS RSS_GROUP (gid, name unique);');
		tx.executeSql('SELECT * FROM RSS_GROUP',[],querySuccess,errorCB);
		},errorCB,function(){
	});
	
}


var addGroup = function(obj){
	showPopupGroup();
}

function showPopupGroup(){
	var db = window.openDatabase("RssDB", "1.0", "Rss DB", 1000000);
	
	$("#jQUi").popup({
		title:"添加组",
		message:"Rss组: <input id='inputGroupName' class='jq-ui-forms' />",
		cancelText:"取消",
		cancelCallback: function(){},
		doneText:"确定",
		doneCallback: function(){
			if(document.getElementById("inputGroupName").value.length!==0)
				db.transaction(function(tx){
					tx.executeSql('CREATE TABLE IF NOT EXISTS RSS_GROUP (gid, name unique);');
					tx.executeSql('INSERT INTO RSS_GROUP (gid, name) VALUES (\'' +  (new Date()).valueOf() +'\',\'' + document.getElementById("inputGroupName").value + '\');');
			},errorCB,function(){
				onCustomizeclick();
				RssHelper.toast("添加成功!");
			});
		},
		cancelOnly:false
		});
	}

function showPopupUrl(obj){
	var db = window.openDatabase("RssDB", "1.0", "Rss DB", 1000000);
	$("#jQUi").popup({
		title:"添加RSS",
		message:"订阅名: <input id='inputRssName' class='jq-ui-forms' /><br />" +
				"订阅url: <input id='inputRssUrl' class='jq-ui-forms' />",
		cancelText:"取消",
		cancelCallback: function(){},
		doneText:"确定",
		doneCallback: function(){
			db.transaction(function(tx){
				tx.executeSql('CREATE TABLE IF NOT EXISTS RSS_URL (uid, gid, name unique, url unique);');
				tx.executeSql('INSERT INTO RSS_URL (uid, gid, name, url) VALUES (\'' +  (new Date()).valueOf() +'\',\'' + obj.parentNode.parentNode.id +
						'\',\'' + document.getElementById("inputRssName").value + '\',\'' + document.getElementById("inputRssUrl").value + '\');');
			},errorCB,function(){
				onCustomizeclick();
				RssHelper.toast("添加成功!");
			});
		},
		cancelOnly:false
		});
	}

var showGroup = function(groupid,header,db,displayStutas) {
//	$("#" + groupUlid).empty();
	var groupDiv = header.parentNode;
	  var urlList = document.createElement('ul');
	  urlList.style.display = displayStutas;
	  urlList.setAttribute('id','_url_List'+groupid);
	  
		function querySuccess(tx, results){
			if((typeof results.rows)!=="undifined"&&results.rows.length>0){
				
				for (var i=0; i<results.rows.length; i++){
					  var li = document.createElement('li');
					  li.innerHTML = "<a onclick='operateUrl(" + results.rows.item(i).uid +")' class='button'>"
									  + results.rows.item(i).name + ": " + results.rows.item(i).url
									  + "</a>";
					  urlList.appendChild(li);
				}} 
				  var li = document.createElement('li');
				  li.setAttribute('id',groupid);
				  li.innerHTML = "<div><a class='button' onclick='showPopupUrl(this);'>添加URL</a>" +
				  		"<a class='button' onclick='deleteGroup(this);'>删除组</a></div>";
				  urlList.appendChild(li);
				  groupDiv.appendChild(urlList);
			if (!results.rowsAffected) {
			  return false;
			}
			$("#customizePanel").show();
		}
		
		db.transaction(function(tx){
			tx.executeSql('CREATE TABLE IF NOT EXISTS RSS_URL (uid, gid, name unique, url unique);');
			tx.executeSql('SELECT * FROM RSS_URL WHERE gid=\''+groupid+'\'',[],querySuccess,errorCB);
			},
			errorCB,
			function(){
			});
}

function operateUrl(uid) {
	 $("#jQUi").actionsheet('<a href="javascript:;" onclick="deleteUrl(\'' + uid + '\');" >删除</a><a href="javascript:;" onclick="modifyUrl(\'' + uid + '\');">修改</a>');
}

function deleteUrl(uid) {
//	alert("delete url: uid -> " + uid);
	var db = window.openDatabase("RssDB", "1.0", "Rss DB", 1000000);
	db.transaction(function(tx){
		tx.executeSql('DELETE FROM RSS_URL WHERE uid=\'' + uid + '\'');
		},
		errorCB,
		function(){
			onCustomizeclick();
		});
}

function modifyUrl(uid) {
//	alert("modify url: uid -> " + uid);
	var db = window.openDatabase("RssDB", "1.0", "Rss DB", 1000000);
	
	var url = "default";
	var name = "default";
	db.transaction(
		function(tx){
		tx.executeSql(
				'SELECT * FROM RSS_URL WHERE RSS_URL.uid=\''+uid+'\'',
				[], 
				function (tx, results){
					if((typeof results.rows)!=="undifined"&&results.rows.length>0){
						url = results.rows.item(0).url;
						name = results.rows.item(0).name;
					}
//					alert("popup");
					$("#jQUi").popup({
						title:"添加RSS",
						message:"订阅名: <input id='modifyInputRssName' class='jq-ui-forms' value='" + name +"'/><br />" +
								"订阅url: <input id='modifyInputRssUrl' class='jq-ui-forms' value='" + url +"'/>",
						cancelText:"取消",
						cancelCallback: function(){},
						doneText:"确定",
						doneCallback: function(){
							db.transaction(function(tx){
								tx.executeSql('UPDATE RSS_URL SET name = \'' +  document.getElementById("modifyInputRssName").value +'\', url = \''
										+ document.getElementById("modifyInputRssUrl").value +
										'\' where uid = \'' + uid + '\';');
							},
							function() {
								RssHelper.toast("修改错误!");
							},
							function(){
								onCustomizeclick();
								RssHelper.toast("修改成功!");
							});
						},
						cancelOnly:false
						});
				},
				errorCB);
		},
		errorCB,
		function(){
		});
	

}

var changingSelect = function(obj,i) {
	if($(obj).attr('class') === "expanded selected"){
		$.expendingLi = "";
	}
	else if($(obj).attr('class') === "collapsed selected") {
		$.expendingLi = "" + i;
	}
}

var deleteGroup = function(obj) {
	var db = window.openDatabase("RssDB", "1.0", "Rss DB", 1000000);
	
	$("#jQUi").popup({
		title:"添加RSS",
		message:"是否删除该组？",
		cancelText:"取消",
		cancelCallback: function(){},
		doneText:"确定",
		doneCallback: function(){
			db.transaction(
					function(tx){
						tx.executeSql('DELETE FROM RSS_URL WHERE gid=\'' + obj.parentNode.parentNode.id + '\'');	
						tx.executeSql('DELETE FROM RSS_GROUP WHERE gid=\'' + obj.parentNode.parentNode.id + '\'');	
					},
					errorCB,
					function(){
						onCustomizeclick();
					});
		},
		cancelOnly:false
		});
	
}

function errorCB(err) {
	if(err.code === 1)
		RssHelper.toast("数据重名!");
}